package cc.flyflow.web.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 客户令牌 拦截器
 *
 * @author Jyl
 */
@Slf4j
@Component
public class CustomInterceptor implements HandlerInterceptor
{

    @Value("${sa-token.timeout}")
    private Long timeout;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod()))
        {
            return true;
        }
        //刷新令牌
        if (StpUtil.getTokenTimeout() < 36000)
        {
            StpUtil.renewTimeout(timeout);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    {

    }
}
